const axios = require('axios');

(async () => {
  console.log('🔍 深入调试特定勋章问题');
  
  try {
    // 1. 登录用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 用户登录成功: ID=${userId}`);
    
    // 2. 获取所有活动列表
    const activitiesResponse = await axios.get('http://localhost:3000/api/activities');
    const activities = activitiesResponse.data.activities;
    
    // 3. 查找特定活动ID
    console.log('\n🔍 检查特定活动:');
    const targetActivityIds = [9, 10, 11];
    
    const targetActivities = activities.filter(activity => 
      targetActivityIds.includes(activity.id)
    );
    
    if (targetActivities.length > 0) {
      console.log(`找到 ${targetActivities.length} 个目标活动:`);
      targetActivities.forEach(activity => {
        console.log(`活动ID ${activity.id}: ${activity.title}`);
        console.log(`  状态: ${activity.status}`);
        console.log(`  开始时间: ${activity.start_time}`);
        console.log(`  结束时间: ${activity.end_time}`);
        console.log(`  创建者: ${activity.creator?.username || 'unknown'}`);
      });
    } else {
      console.log(`❌ 未找到ID为 ${targetActivityIds.join(', ')} 的活动`);
      
      // 显示所有活动ID
      console.log('\n所有活动ID:');
      activities.forEach(activity => {
        console.log(`- ID ${activity.id}: ${activity.title}`);
      });
    }
    
    // 4. 获取各类勋章
    console.log('\n🏆 获取勋章数据...');
    
    // "全部勋章"
    const allBadgesResponse = await axios.get('http://localhost:3000/api/badges', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const allBadges = allBadgesResponse.data.badges;
    
    // "可获得勋章"
    const availableBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=available', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const availableBadges = availableBadgesResponse.data.badges;
    
    // 5. 查找特定的potential_勋章
    console.log('\n🔍 检查特定勋章:');
    const targetBadgeIds = ['potential_9', 'potential_10', 'potential_11'];
    
    // 在全部勋章中查找
    const targetBadgesInAll = allBadges.filter(badge => 
      targetBadgeIds.includes(badge.id)
    );
    
    console.log(`在"全部勋章"中找到 ${targetBadgesInAll.length} 个目标勋章`);
    
    // 在可获得勋章中查找
    const targetBadgesInAvailable = availableBadges.filter(badge => 
      targetBadgeIds.includes(badge.id)
    );
    
    console.log(`在"可获得勋章"中找到 ${targetBadgesInAvailable.length} 个目标勋章`);
    
    if (targetBadgesInAvailable.length > 0) {
      console.log('\n找到的目标勋章详情:');
      targetBadgesInAvailable.forEach(badge => {
        console.log(`勋章ID: ${badge.id}`);
        console.log(`  名称: ${badge.badge_name}`);
        console.log(`  活动ID: ${badge.activity_id}`);
        console.log(`  状态: ${badge.status}`);
        console.log(`  是否已获得: ${badge.isOwned ? '是' : '否'}`);
      });
    }
    
    // 6. 仔细检查分页问题
    console.log('\n🔍 检查分页问题:');
    
    // 不分页获取所有全部勋章
    const allBadgesNoPaginationResponse = await axios.get('http://localhost:3000/api/badges?limit=100', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const allBadgesNoPagination = allBadgesNoPaginationResponse.data.badges;
    
    console.log(`不分页获取的全部勋章数量: ${allBadgesNoPagination.length}`);
    
    // 在不分页的结果中查找目标勋章
    const targetBadgesInAllNoPagination = allBadgesNoPagination.filter(badge => 
      targetBadgeIds.includes(badge.id)
    );
    
    console.log(`在不分页的全部勋章中找到 ${targetBadgesInAllNoPagination.length} 个目标勋章`);
    
    // 7. 诊断潜在问题
    console.log('\n📋 问题诊断:');
    
    if (targetBadgesInAll.length !== targetBadgesInAvailable.length) {
      console.log('❌ 目标勋章在"全部勋章"和"可获得勋章"中的数量不一致');
      console.log(`  - 全部勋章中: ${targetBadgesInAll.length}个`);
      console.log(`  - 可获得勋章中: ${targetBadgesInAvailable.length}个`);
    }
    
    if (allBadges.length < allBadgesNoPagination.length) {
      console.log('❌ 可能是分页问题');
      console.log(`  - 默认分页返回: ${allBadges.length}个`);
      console.log(`  - 不分页返回: ${allBadgesNoPagination.length}个`);
    }
    
    // 8. 找出"全部勋章"中未显示的勋章在集合中的位置
    if (targetBadgesInAvailable.length > 0 && targetBadgesInAll.length < targetBadgesInAvailable.length) {
      console.log('\n🔍 分析未显示勋章在集合中的位置:');
      
      // 按activity_id对所有勋章排序
      const sortedBadges = [...allBadgesNoPagination].sort((a, b) => {
        const aId = a.activity_id || 0;
        const bId = b.activity_id || 0;
        return aId - bId;
      });
      
      console.log('勋章在集合中的排序:');
      sortedBadges.forEach((badge, index) => {
        if (badge.activity_id >= 8 && badge.activity_id <= 12) {
          console.log(`位置 ${index}: 勋章ID ${badge.id}, 活动ID ${badge.activity_id}, 名称: ${badge.badge_name}`);
        }
      });
    }
    
  } catch (error) {
    console.error('❌ 调试过程出错:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 